import { defineStore } from 'pinia'
import { reactive } from "vue"
import {
    getSupplierAll, getProductList
} from "@/api/product"
import { merchantListApiPost } from "@/api/userMangement"

import { systemMessageListPost } from "@/api/serviceSystem"
import { totalProductPagePost } from "@/api/allProduct"
import { equityListPost, schemeListPost, equityPackageListGet, EquityPortfolioPagePost, voucherSaveOrUpdateListPost } from "@/api/equity"
import { dictPagePost, dicQueryDictInfoApiGet } from "@/api/operationManagement"
import { useEnumStore } from "./enumeration"
export const useComListData = defineStore('useComListDataId', () => {
    // 这个数据要更新 和什么有关系 add edit del
    // 供应商列表
    const { supplyerType } = useEnumStore()
    const listCollect = reactive({
        supplierList: [] as any, // 供应商
        equityList: [] as any,// 立减金
        messageList: [] as any, // 消息
        productList: [] as any, // 产品列表
        equityPackageList: [] as any, // 权益包

        equitySchemeList: [] as any, // 方案配置
        merchantList: [] as any, // 渠道商
        envList: [] as any,// 环境
        sceneList: [] as any,// 场景


        totalProduct: [] as any,// 产品总分类
        equityPortfolioList: [] as any,// 权益组合
        virtualList: [] as any,// 虚拟会员字典值
        virtualMemberList: [] as any,// 虚拟会员list列表
        reportingPlatformCodeList: [] as any,// 上报平台
    })

    const getEquityPortfolioList = async () => {
        const listSearch = reactive({
            condition: {
                packageCode: null,//包编码
                portfolioCode: null,//组合编码
                portfolioName: null,//组合名称
                equityType: null,//权益类型
            },
            orders: ["created_time desc"],
            pageNum: 20000,
            pageSize: 1
        });
        EquityPortfolioPagePost(listSearch).then((res) => {
            if (res.code == 20000) {
                listCollect.equityPortfolioList = res.data.records
            }
        });
    }


    // 虚拟会员领取
    const getVirtualType = async (code: string) => {
        try {
            const res = await dicQueryDictInfoApiGet(code)
            console.log(res, 'dicode');

            if (res.code == 20000) {
                listCollect.virtualList = res.data && res.data.dictItemList && res.data.dictItemList.map((item: any) => {
                    return {
                        ...item,
                        key: item.dictItemCode,
                        value: item.dictItemName
                    }
                })

            }
        } catch (error) {
            console.log(error);

        }
    }
    const getEnvList = async () => {
        const listSearch = reactive({
            orders: ["created_time desc"],
            condition: {
                dictCode: 'ENVIRONMENT'
            },
            pageSize: 200000,
            pageNum: 1
        });
        try {
            const res = await dictPagePost(listSearch)
            if (res.code == 20000) {
                listCollect.envList = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                        key: item.dictItemCode,
                        value: `${item.dictItemCode} - ${item.dictItemName}`,
                        code: item.dictItemCode,
                        label: item.dictItemName
                    }
                })
            }
        } catch (error) {
            console.log(error);
        }
    }
    const getReportingPlatformCode = async () => {
        const listSearch = reactive({
            orders: ["created_time desc"],
            condition: {
                dictCode: 'REPORTING_PLATFORM'
            },
            pageSize: 200000,
            pageNum: 1
        });
        try {
            const res = await dictPagePost(listSearch)
            if (res.code == 20000) {
                listCollect.reportingPlatformCodeList = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                        key: item.dictItemCode,
                        value: item.dictItemName
                    }
                })
            }
        } catch (error) {
            console.log(error);
        }
    }
    const getSceneList = async () => {
        const listSearch = reactive({
            orders: ["created_time desc"],
            condition: {
                dictCode: 'SCENE'
            },
            pageSize: 200000,
            pageNum: 1
        });
        try {
            const res = await dictPagePost(listSearch)
            if (res.code == 20000) {
                listCollect.sceneList = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                        key: item.dictItemCode,
                        value: `${item.dictItemCode} - ${item.dictItemName}`,
                        code: item.dictItemCode,
                        label: item.dictItemName
                    }
                })
            }
        } catch (error) {
            console.log(error);

        }
    }
    const getMerchantList = async () => {
        try {
            const res = await merchantListApiPost({})
            if (res.code == 20000) {
                listCollect.merchantList = res.data && res.data.map((item: any) => {
                    return {
                        ...item,
                        // key:
                        key: item.channelMerchantCode,
                        value: `${item.channelMerchantCode} - ${item.channelMerchantName}`,
                        id: item.id,
                        label: item.channelMerchantName
                    }
                })
            }
        } catch (error) {
            console.log(error);

        }
    }
    const getSupplierAllList = async (search?: any) => {

        let res = await getSupplierAll({
            supplierType: search ? search : ''
        })
        if (res.code == 20000) {
            listCollect.supplierList = res.data && res.data.map((item: any) => {
                return {
                    key: item.supplierCode,
                    value: `${item.supplierKey} - ${item.supplierName}`,
                    id: item.id,
                    label: item.supplierName
                }
            })
        }
    }
    /*所有产品*/
    const getProListAll = async (params?: any) => {
        // 产品
        const searchParams =  JSON.stringify(params) === '{}' ? {} : params
        let initParams = ref({
            pageNum: 1,
            pageSize: 20000,
            condition:searchParams,
            orders: ["created_time desc"]
          })
        let res = await getProductList(initParams.value);

        if (res.code == 20000) {
            listCollect.productList = res.data.records && res.data.records.map((item: any) => {
                return {
                    ...item,
                    key: item.productCode,
                    value: `${item.productCode} - ${item.productName}`,
                    id: item.id,
                    label: item.productName
                };
            });
        }
    };
    const getEquity = async () => {
        try {
            const arr = {
                supplierCode: null,//
                enabled: 1,
                supplierReductionVoucherCode: null,//
                reductionVoucherTypeList: null,//
                reductionVoucherCode: null,//
                reductionVoucherName: null,//
                reductionVoucherAmount: null,//
            }
            const res = await equityListPost(arr)
            listCollect.equityList = res.data && res.data.map((item: any) => {
                return {
                    key: item.reductionVoucherCode,
                    value: `${item.reductionVoucherCode} - ${item.reductionVoucherName}`,
                    id: item.reductionVoucherName,
                    money: item.reductionVoucherAmount,
                    objCode: item.reductionVoucherCode,
                    objName: item.reductionVoucherName
                }
            })
        } catch (error) {
            console.log(error);

        }
    }
    const getMessageList = async () => {
        const arr = {
            templateCode: null,
            templateName: null,
            templateType: null,
        }
        try {
            const res = await systemMessageListPost(arr)
            if (res.code == 20000) {
                listCollect.messageList = res.data && res.data.map((item: any) => {
                    return {
                        ...item,
                        key: item.templateCode,
                        value: `${item.templateName} - ${item.content}`,
                    }
                })
            }
        } catch (error) {
            console.log(error);
        }
    }
    const getEquityList = async () => {
        try {
            const arr = {
                packageCode: "",
                packageName: "",
                packageUsageMode: "",
                remarks: "",
            }
            const res = await equityPackageListGet(arr)
            listCollect.equityPackageList = res.data && res.data.map(((item: any) => {
                return {
                    ...item,
                    key: item.packageCode,
                    value: `${item.packageCode} - ${item.packageName}`
                }
            }))
        } catch (error) {
            console.log(error);

        }
    }
    const getSchemeList = async () => {
        const arr = {
            schemeCode: "",
            schemeName: "",
            schemeType: "",
        }
        try {
            const res = await schemeListPost(arr)
            if (res.code == 20000) {
                listCollect.equitySchemeList = res.data && res.data.map((item: any) => {
                    return {
                        ...item,
                        key: item.schemeCode,
                        value: `${item.schemeCode} - ${item.schemeName}`
                    }
                })
            }
        } catch (error) {
            console.log(error);

        }
    }
    /* 1 */
    const getAllTotalProduct = async () => {
        const listSearch = reactive({
            orders: ["id"],
            condition: {
                productCategoryName: null,//产品总称 
                productCategoryCode: null,//产品编码 
                productType: null,//产品类型 
                remarks: null,//供应商编码 
                id: null,//序号 
            },
            pageSize: 20,
            pageNum: 1
        });
        try {
            const res = await totalProductPagePost(listSearch)
            if (res.code == 20000) {
                listCollect.totalProduct = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                        key: item.productCategoryCode,
                        value: `${item.productCategoryCode} - ${item.productCategoryName}`,
                        label: item.productCategoryName
                    }
                })
            }
        } catch (error) {
            console.log(error);

        }
    }

    const getvirtualMemberList = async (params?: any) => {
        let initParams = {
            supplierCode: null,
            equityVoucherType: null,
            equityVoucherCode: null,
            equityVoucherName: null,
            supplierEquityVoucherCode: null
        };
        Object.assign(initParams, params)
        voucherSaveOrUpdateListPost(initParams).then((res) => {
            if (res.code == 20000) {
                listCollect.virtualMemberList = res.data && res.data.map((item: any) => {
                    return {
                        ...item,
                        key: item.equityVoucherCode,
                        value: `${item.equityVoucherCode} - ${item.equityVoucherName}`,
                        objCode: item.equityVoucherCode,
                        objName: item.equityVoucherName
                    }
                })
            }
        });
    }
    return {
        listCollect,
        getSupplierAllList, getEquity, getMessageList, getProListAll, getEquityList, getSchemeList, getMerchantList,
        getSceneList, getEnvList, getAllTotalProduct, getEquityPortfolioList, getVirtualType, getvirtualMemberList, getReportingPlatformCode
    }
})